package com.yuecai.modules.yc_order.service.utils;

import com.yuecai.modules.yc_goods.domain.YcGoods;
import com.yuecai.modules.yc_goods.vo.YcGoodsOrderVo;
import com.yuecai.modules.yc_user_coupon.domain.YcUserCoupon;
import com.yuecai.modules.yc_user_coupon.repository.YcUserCouponRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: dm
 * @Date: 2020/7/27 10:53
 * @Description:
 */
@Component
public class CoupontUtils {

    @Autowired
    private YcUserCouponRepository userCouponRepository;

    /**
     * 查詢可用优惠券
     * @param ycGoods
     * @return
     */
    public List<YcUserCoupon> findCoupont(List<YcGoodsOrderVo> ycGoods, Long userId, BigDecimal total){
        List<YcUserCoupon> byUserId = userCouponRepository.findByUserIdAndIsUse(userId,0);
        if (CollectionUtils.isNotEmpty(byUserId)){
            List<YcUserCoupon> ycUserCoupons = new ArrayList<>();
            for (YcUserCoupon item : byUserId) {
                //失效时间 时间戳
                Timestamp expireDate = item.getExpireDate();
                Timestamp now = new Timestamp(System.currentTimeMillis());
                if (now.after(expireDate)){
                    continue;
                }
                //条件的类型，0为金额，1为件数
                Integer conditionType = item.getConditionType();
                BigDecimal conditions = item.getConditions();
                if (conditionType==0){
                    if (conditions.compareTo(total) == 1){
                        continue;
                    }
                }else if (conditionType==1){
                    int sum = ycGoods.stream().filter(a -> a.getCount() != null).mapToInt(YcGoodsOrderVo::getCount).sum();
                    if (BigDecimal.valueOf(sum).compareTo(conditions) == -1){
                        continue;
                    }
                }
                //1:全品类 2:限品类 3:限制品牌
                Integer couponType = item.getCouponType();
                Long conditionsId = item.getConditionsId();
                if (couponType==1){
                    ycUserCoupons.add(item);
                    continue;
                }
                Set<Long> set = null;
                if(couponType==2){
                    set = ycGoods.stream().map(YcGoods::getCategoryId).collect(Collectors.toSet());
                }else if (couponType==3){
                    set = ycGoods.stream().map(YcGoods::getBrandId).collect(Collectors.toSet());
                }
                boolean contains = set.contains(conditionsId);
                if (!contains){
                    continue;
                }
                ycUserCoupons.add(item);
            }
            return ycUserCoupons;
        }

        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public YcUserCoupon isUse(Long couponId, List<YcGoodsOrderVo> vos, BigDecimal total) {
        if (couponId == null){
            return null;
        }
        Optional<YcUserCoupon> byId = userCouponRepository.findById(couponId);
        if (!byId.isPresent()){
            return null;
        }
        YcUserCoupon item = byId.get();
        Integer isUse = item.getIsUse();
        if (isUse!=0){
            return null;
        }
        //失效时间 时间戳
        Timestamp expireDate = item.getExpireDate();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if (now.after(expireDate)){
            return null;
        }
        //条件的类型，0为金额，1为件数
        Integer conditionType = item.getConditionType();
        BigDecimal conditions = item.getConditions();
        if (conditionType==0){
            if (conditions.compareTo(total) > -1){
                return null;
            }
        }else if (conditionType==1){
            int sum = vos.stream().filter(a -> a.getCount() != null).mapToInt(YcGoodsOrderVo::getCount).sum();
            if (BigDecimal.valueOf(sum).compareTo(conditions) == -1){
                return null;
            }
        }
        //1:全品类 2:限品类 3:限制品牌
        Integer couponType = item.getCouponType();
        Long conditionsId = item.getConditionsId();
        if (couponType!=1){
            Set<Long> set = null;
            if(couponType==2){
                set = vos.stream().map(YcGoods::getCategoryId).collect(Collectors.toSet());
            }else if (couponType==3){
                set = vos.stream().map(YcGoods::getBrandId).collect(Collectors.toSet());
            }
            boolean contains = set.contains(conditionsId);
            if (!contains){
                return null;
            }
        }
//        item.setIsUse(2);
//        userCouponRepository.save(item);
        return item;
    }


    public YcUserCoupon showIsUse(Long couponId, List<YcGoodsOrderVo> vos, BigDecimal total) {
        Optional<YcUserCoupon> byId = userCouponRepository.findById(couponId);
        if (!byId.isPresent()){
            return null;
        }
        YcUserCoupon item = byId.get();
        Integer isUse = item.getIsUse();
        if (isUse!=0){
            return null;
        }
        //失效时间 时间戳
        Timestamp expireDate = item.getExpireDate();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if (now.after(expireDate)){
            return null;
        }
        //条件的类型，0为金额，1为件数
        Integer conditionType = item.getConditionType();
        BigDecimal conditions = item.getConditions();
        if (conditionType==0){
            if (conditions.compareTo(total) > -1){
                return null;
            }
        }else if (conditionType==1){
            int sum = vos.stream().filter(a -> a.getCount() != null).mapToInt(YcGoodsOrderVo::getCount).sum();
            if (BigDecimal.valueOf(sum).compareTo(conditions) == -1){
                return null;
            }
        }
        //1:全品类 2:限品类 3:限制品牌
        Integer couponType = item.getCouponType();
        Long conditionsId = item.getConditionsId();
        if (couponType!=1){
            Set<Long> set = null;
            if(couponType==2){
                set = vos.stream().map(YcGoods::getCategoryId).collect(Collectors.toSet());
            }else if (couponType==3){
                set = vos.stream().map(YcGoods::getBrandId).collect(Collectors.toSet());
            }
            boolean contains = set.contains(conditionsId);
            if (!contains){
                return null;
            }
        }
        return item;
    }
}
